package com.mobile.challenge.user

import com.mobile.challenge.bean.DataBean
import com.mobile.challenge.bean.TrackingBean
import com.mobile.challenge.bean.UserBean
import com.mobile.challenge.util.*

/**
 * @Author Touge
 * @Date 2020/9/17 15:47
 * @Description 只能在 Report 页面显示出来之后才有用
 */
class UserHelper {
    private var userBean: UserBean? = null

    private var dataBean: DataBean? = null

    companion object {
        @Volatile
        private var instance: UserHelper? = null

        fun getInstance(): UserHelper {
            return instance ?: synchronized(this) {
                instance ?: UserHelper().also { instance = it }
            }
        }
    }

    fun setUser(dataBean: DataBean) {
        this.dataBean = dataBean
        this.userBean = dataBean.user
    }

    fun isStaff(): Boolean {
        return if (userBean != null) {
            userBean!!.roleId == UserType.STUFF
        } else {
            true
        }
    }

    fun isManager(): Boolean {
        return if (userBean != null) {
            userBean!!.roleId == UserType.MANAGER
        } else {
            false
        }
    }

    fun getUserType(): Int {
        return if (userBean != null) {
            userBean!!.roleId
        } else {
            UserType.STUFF
        }
    }

    fun getUserId(): Int {

        return if (userBean == null) {
            103
        } else {
            userBean!!.id
        }
    }

    fun hasSignIn(): Boolean {
        return dataBean!!.tracking != null
    }

    fun getUserName(): String {
        return userBean!!.name
    }

    fun getLocationStatus(status: String): Int {
        return when (status) {
            WFH -> {
                2
            }
            IN_OFFICE -> {
                1
            }
            VACATION -> {
                3
            }
            else -> {
                1
            }
        }
    }

    fun getLocation(status: Int): String {
        return when (status) {
            2 -> {
                WFH
            }
            1 -> {
                IN_OFFICE
            }
            3 -> {
                VACATION
            }
            else -> {
                NO_DATA
            }
        }
    }

    fun getTrackingStatus(): Int {
        return dataBean?.user?.status ?: -1
    }

    fun saveTracking(trackingBean: TrackingBean?) {
        if (trackingBean == null) {
            return
        }
        if (DateUtil.isToday(trackingBean.date)) {
            dataBean?.tracking = trackingBean
        }
    }
}